package com.etoak.controller;


import com.etoak.common.core.vo.PageVO;
import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.Ingredients;
import com.etoak.service.IngredientsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author etoak
 * @since 2025-03-04
 */
@RestController
@RequestMapping("/ingredients")
public class IngredientsController {
    @Autowired
    IngredientsService ingredientsService;

    /**
     * 添加食材接口 post/ingredients
     */
    @PostMapping
    public ResultVO<Object> add(@Validated @RequestBody Ingredients ingredients) {
        ingredientsService.add(ingredients);
        return ResultVO.success();
    }
    /**
     * 分页查询get/ingredients/list
     */
    @GetMapping("/list")
    public ResultVO<PageVO<Ingredients>> list(Ingredients ingredients){
        PageVO<Ingredients> pageVO=ingredientsService.list(ingredients);
        return ResultVO.success(pageVO);
    }
    /**
     * 更新食材接口 put /ingredients/{id}
     */
    @PutMapping("/{id}")
    public ResultVO<Object> update(@PathVariable int id,@RequestBody Ingredients ingredients){
        ingredientsService.update(id,ingredients);
        return ResultVO.success();
    }

    /**
     * 删除接口 delete /ingredients/{id}
     */
    @DeleteMapping("/{id}")
    public ResultVO<Object> delete(@PathVariable int id){
        ingredientsService.delete(id);
        return ResultVO.success();
    }

    /**
     * 根据食材类型获取食材列表
     * @param type
     * @return
     */
    @GetMapping("/type/{type}")
    public ResultVO<List<Ingredients>> getByType(@PathVariable String type){
        List<Ingredients> ingredientsList=ingredientsService.listByType(type);
        return ResultVO.success(ingredientsList);
    }
}

